package com.qf.openapi.web.master.intercepter;

import com.qf.openapi.web.master.entity.User;
import com.qf.openapi.web.master.utils.Contants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Slf4j
public class LoginIntercepector implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("拦截到url:{}",request.getRequestURI());

        //判断用户是否登录过
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(Contants.SESSION_USER_KEY);
        if (user == null){
            log.debug("没有登录，重定向到登录页面");
            response.sendRedirect("/login.html");
            return  false;
        }
        log.debug(",session中已经有user对象，所以放行放行");

        return true;
    }
}
